package edu.buaa.energy.transaction;

import com.alibaba.fastjson.annotation.JSONField;
import edu.buaa.common.transaction.AbstractTransaction;
import edu.buaa.utils.Helper;
import edu.buaa.utils.Triple;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

public class SnapshotAggrDurationTx extends AbstractTransaction {
    private int t0;
    private int t1;
    private String p;
    private List<Float> intervalStarts;

    public SnapshotAggrDurationTx() {
        this.setTxType(TxType.tx_query_snapshot_aggr_duration);
    }

    public int getT0() {
        return t0;
    }

    public int getT1() {
        return t1;
    }

    public String getP() {
        return p;
    }

    public void setT0(int t0) {
        this.t0 = t0;
    }

    public void setT1(int t1) {
        this.t1 = t1;
    }

    public void setP(String p) {
        this.p = p;
    }

    @JSONField(serialize = false)
    public TreeSet<Float> getIntStartTreeSet() {
        return new TreeSet<>(intervalStarts);
    }

    public List<Float> getIntervalStarts() {
        return intervalStarts;
    }

    public void setIntervalStarts(List<Float> intervalStarts) {
        this.intervalStarts = intervalStarts;
    }

    public static class Result extends AbstractTransaction.Result {
        List<Triple<Integer, Float, Integer>> statusDuration;

        public List<Triple<Integer, Float, Integer>> getStatusDuration() {
            return statusDuration;
        }

        public void setStatusDuration(List<Triple<Integer, Float, Integer>> statusDuration) {
            this.statusDuration = statusDuration;
        }
    }

    @Override
    public boolean validateResult(AbstractTransaction.Result result) {
        return Helper.validateResult(conv(((Result) this.getResult()).getStatusDuration()), conv(((Result) result).getStatusDuration()));
    }

    private List<Triple<Integer, Integer, Integer>> conv(List<Triple<Integer, Float, Integer>> from){
        return from.stream().map(t3 -> Triple.of(t3.getLeft(), Math.round(t3.getMiddle() * 1000), t3.getRight())).distinct().collect(Collectors.toList());
    }
}
